package com.zbvc.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zbvc.dao.AdminDao;
import com.zbvc.dao.impl.AdminDaoImpl;
import com.zbvc.entity.Admin;

/**
 * Servlet implementation class UpdateUserServlet
 */
@WebServlet(name = "updateUser", urlPatterns = { "/updateUser" })
public class UpdateUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateUserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 处理中文乱码
				request.setCharacterEncoding("UTF-8");
				response.setContentType("text/html;charset=utf-8");
				
				// 获取需要修改的字段值  8
				int aid = Integer.parseInt(request.getParameter("aid"));
				String name = request.getParameter("name");
				String username = request.getParameter("username");
				String password = request.getParameter("password");
				String phone = request.getParameter("password");
				String email = request.getParameter("email");
				int lend_num = Integer.parseInt(request.getParameter("lend_num"));
				int max_num = Integer.parseInt(request.getParameter("max_num"));
				// 将八个字段值封装到Admin
				Admin admin = new Admin();
				admin.setAid(aid);
				admin.setLend_num(lend_num);
				admin.setEmail(email);
				admin.setMax_num(max_num);
				admin.setPassword(password);
				admin.setPhone(phone);
				admin.setUsername(username);
				admin.setName(name);
				// 调用AdminDao中的updateUserById(Admin admin)方法
				AdminDao adminDao = new AdminDaoImpl();
				int row =adminDao.updateUserById(admin);
				System.out.println("更新用户"+row+"条");
				//跳转到用户读者管理
				response.sendRedirect("/bookManager/admin/admin_user.jsp");
				
				
	}

}
